//
//  CatShareListService.m
//  YinHang
//
//  Created by idea on 14-10-17.
//  Copyright (c) 2014年 孙彦鹏. All rights reserved.
//

#import "CatShareListService.h"

@implementation CatShareListService{
    ASIFormDataRequest *request_;
    ASIFormDataRequest *getName;
    NSDictionary *dic;
    NSString *mid;
    NSString *name;
}

- (void)dealloc {
    [request_ cancel];
    
    NSLog(@"CatListService dealloc...");
}

- (void)pageQueryWithSelectedIndex:(NSInteger)selectedIndex selectedTid:(NSString *)selectedTid{
    
    if (_isQuery || ([self.pageInfo.pageno integerValue] > 1 && !self.pageInfo.hasMoreData)) {
        return;
    }
    
    /* 获取全部类别 */
    
    // 检查网络
    if (![Constants isExistenceNetwork]) {
        
    } else {
        request_ = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:API_STR]];
        request_.requestMethod = @"POST";
        // 设置超时时间
        request_.timeOutSeconds = 30;
        request_.delegate = self;
        // 设置参数
        NSString *action = @"gongxianglist";
        switch (selectedIndex) {
            case 0://新闻
                mid = @"1";
                //action = @"newstypelist";
                break;
            case 1://人才
                 mid = @"2";
                //action = @"zhaopintypelist";
                break;
            case 2://视频
                 mid = @"3";
                //action = @"videotypelist";
                break;
            case 3://产品
                 mid = @"4";
                //action = @"prostypelist";
                break;
            case 4://图片
                 mid = @"5";
                //action = @"casetypelist";
                break;
            case 5://服务
                 mid = @"6";
                //action = @"serinfortypelist";
                break;
                
            default:
                break;
        }
       // NSString *tid = [NSString stringWithFormat:@"%i",selectedTid];
        //NSDictionary *paramsDict =  @{@"uid": @"互联天下",@"pagenow": self.pageInfo.pageno, @"pagesize": self.pageInfo.pagesize};
        NSDictionary *paramsDict =  @{@"tid":selectedTid,@"mid":mid,@"pagenow": self.pageInfo.pageno, @"pagesize": self.pageInfo.pagesize};
        NSString *paramsStr = [NSString JSONString:paramsDict];
        NSString *result = [NSString stringWithFormat:TEMPLATE_PARAMSTR, action,paramsStr];
        
        NSData *paramsData = [result toGBKEncodingData];
        request_.postBody = [paramsData mutableCopy];
        
        [request_ startAsynchronous];
        
        
        _isQuery = YES;
    }
}

#pragma mark - Property method
- (CatSharePageInfo*)pageInfo{
    if (!_pageInfo) {
        _pageInfo = [[CatSharePageInfo alloc]init];
    }
    return _pageInfo;
}

#pragma mark - ASIHttpRequestDelegate method

- (void)requestFinished:(ASIHTTPRequest *)request {
    NSLog(@"%@",request.responseString);
    NSError *err;
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[request.responseString dataForUTF8] options:NSJSONReadingAllowFragments error:&err];
    
    // 1. 是否解析有错误
    if (!err) {
        // 2. 判断错误码
        NSInteger errorCode = [result[@"errorCode"]integerValue];
        if (errorCode == 0) {
            // 2.1 获取"info"
            NSDictionary *info = result[@"info"];
            //NSLog(@"%@",info);
            //NSInteger totalCount = [info[@"total"]integerValue];
            NSArray *list = info[@"data"];
            if (request == request_) {
                
                if ([self.pageInfo.pageno isEqualToString:@"1"]) {
                    [self.pageInfo.dataArr removeAllObjects];
                }
                [self.pageInfo.dataArr addObjectsFromArray:list];
                
                //转换cid为用户名
                for (int i = 0; i < self.pageInfo.dataArr.count ; i++) {
                    NSString *nameStr = [self getUserNameWith:self.pageInfo.dataArr[i][@"cid"]];
                    if (nameStr == nil) {
                        [SVProgressHUD showErrorWithStatus:@"数据出错！"];
                        return;
                    }
                    NSMutableDictionary *dic_ = [self.pageInfo.dataArr[i] mutableCopy];
                    [dic_ setObject:nameStr forKey:@"cid"];
                    self.pageInfo.dataArr[i] = [dic_ mutableCopy];
                }
                
                [_delegate didRecieveData];
                
            }else{
                NSDictionary *info = result[@"info"];
                name = info[@"uid"];
                //name = @"1";
            }
            
            
            
        } else if (errorCode == 1){
            
        } else if (errorCode == 5){
            // [self.pageInfo.dataArr removeAllObjects];
            self.pageInfo.hasMoreData = NO;
            [_delegate didRecieveMsg];
        }else {
            
            NSString *dataStr = result[@"info"][@"data"];
            if ([dataStr isEqualToString:@"0"]) {
                [self.pageInfo.dataArr removeAllObjects];
                self.pageInfo.hasMoreData = NO;
                if ([result[@"errorMsg"] isEqualToString:@"该条件下为得到任何数据"]){
                    [_delegate didRecieveMsg];
                }
            }
        }
        
    } else {
        [SVProgressHUD showErrorWithStatus:@"数据解析出错！"];
        [_delegate didRecieveMsg];
    }
    
    _isQuery = NO;
}

- (void)requestFailed:(ASIHTTPRequest *)request{
    [SVProgressHUD showErrorWithStatus:@"服务器无法访问，请稍后再试!"];
    [_delegate didRecieveMsg];
    _isQuery = NO;
}

// 取消网络访问
- (void)cancelRequest {
    [request_ cancel];
}

- (NSString *)getUserNameWith:(NSString*)cid{
    // 检查网络
    if (![Constants isExistenceNetwork]) {
        
    } else {
        getName = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:API_STR]];
        getName.requestMethod = @"POST";
        // 设置超时时间
        getName.timeOutSeconds = 30;
        getName.delegate = self;
        // 设置参数
        NSString *action = @"zhuan";
        NSDictionary *paramsDict =  @{@"cid":cid};
        NSString *paramsStr = [NSString JSONString:paramsDict];
        NSString *result = [NSString stringWithFormat:TEMPLATE_PARAMSTR, action,paramsStr];
    
        NSData *paramsData = [result toGBKEncodingData];
        getName.postBody = [paramsData mutableCopy];
    
        [getName startSynchronous];
    }
    return name;
}



@end
